
import lucene.server.client.QueryClient;
import lucene.server.client.ClientWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class MainSearchThread {
    //创建客户端
    static QueryClient queryClient = new QueryClient("http://localhost:9999","client","client");
    private static final int THREAD_COUNT = 20;
    private static final int QUERY_COUNT = 100000 / THREAD_COUNT; // 每个线程执行的查询次数

    public static void main(String[] args) throws InterruptedException {
        List<Object> mainKids = new ArrayList<>();
        mainKids.add("3");
        mainKids.add("2");
        mainKids.add("1");
        ClientWrapper clientWrapper = ClientWrapper.create()
                .SELECT("id")
                .From("supervision")
                .ge("cert_date_begin", "2023-01-01")
                .in("main_kind", mainKids)
                .orderBy("main_kind", false);

        ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);

        long startTime = System.currentTimeMillis();

        for (int i = 0; i < THREAD_COUNT; i++) {
            executor.execute(() -> {
                for (int j = 0; j < QUERY_COUNT; j++) {
                    queryClient.request(clientWrapper);
                }
            });
        }

        executor.shutdown();
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);


        long countMillis = System.currentTimeMillis() - startTime;
        System.out.printf("10万次搜索，花费时间: %d 秒 \n", countMillis / 1000);
        System.out.printf("每秒请求: %d 次 \n", 100000 * 1000 / countMillis);
    }
}